package com.lingxu;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: sjmfrh-api
 * @ClassName NoModelDataListener
 * @description:
 * @author: gxy
 * @create: 2025-06-13 17:02
 * @Version 1.0
 **/
@Slf4j
@Data
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
//    private static final int BATCH_COUNT = 5;
//    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private List<Map<Integer, String>> cachedDataList = new ArrayList<>();

    private List<YdDataDetail> dataList = new ArrayList<>();

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
//        System.out.println("解析到一条数据:{}"+JSON.toJSONString(data));
//        cachedDataList.add(data);

        YdDataDetail detail = new YdDataDetail();
        data.forEach((k,v)->{
            if(StringUtils.isBlank(v)) {
                ReflectUtil.setObjFieldValV2(detail, "value" + k, null);
            } else if(v.startsWith("`")) {
                ReflectUtil.setObjFieldValV2(detail, "value" + k, v.replace("`",""));
            } else {
                ReflectUtil.setObjFieldValV2(detail, "value" + k, v);
            }
        });
        dataList.add(detail);
//        if (cachedDataList.size() >= BATCH_COUNT) {
//            saveData();
//            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
//        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
//        saveData();
//        System.out.println("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        System.out.println("{}条数据，开始存储数据库！"+cachedDataList.size());
        System.out.println("存储数据库成功！");
    }
}

